package com.example.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduation.entity.Goods;
import com.example.graduation.entity.Notice;
import com.example.graduation.entity.Shop;
import com.example.graduation.entity.Torder;
import com.example.graduation.exception.MyException;
import com.example.graduation.mapper.GoodsMapper;
import com.example.graduation.mapper.NoticeMapper;
import com.example.graduation.mapper.OrderMapper;
import com.example.graduation.mapper.ShopMapper;
import com.example.graduation.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.graduation.util.JwtUtil;
import com.example.graduation.vo.ShopInfoVo;
import com.example.graduation.vo.ShopVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private NoticeMapper noticeMapper;


    /**
     * 查询商店进去之后的信息，商店信息和商店中商品的信息
     *
     * @param shopId      商店id
     * @param status      商店状态
     * @param type        商品类型
     * @param goodsStatus 商品状态
     * @return {@link ShopInfoVo}
     * @author xieyucan
     * CreateDate 2022/3/2 18:22
     */
    @Override
    public ShopInfoVo selectShopById(Integer shopId, Integer status, Integer type, Integer goodsStatus) {
        try {
            //先把商店信息查询出来
            ShopVo shopVo = shopMapper.selectShopById(shopId, status);
            ShopInfoVo shopInfoVo = new ShopInfoVo();
            //把商店信息拷贝到ShopInfoVo中
            BeanUtils.copyProperties(shopVo, shopInfoVo);
            //查询商店中商品的信息
            List<Goods> goods = goodsMapper.selectGoodsByType(shopVo.getShopId(), shopVo.getStatus(), goodsStatus);
            shopInfoVo.setList(goods);
            return shopInfoVo;
        } catch (Exception e) {
            throw new MyException(e.getMessage());
        }

    }


    /**
     * 查询商店的详细信息
     *
     * @param shopId
     * @return {@link Shop}
     * @author xieyucan
     * CreateDate 2022/3/3 16:37
     */
    @Override
    public Shop selectShopInfo(Integer shopId) {
        try {
            Shop shop = shopMapper.selectShopInfo(shopId);
            return shop;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(e.getMessage());
        }
    }


    /**
     * 查询本人商店的内容
     *
     * @param token
     * @return
     */
    public Shop selectShopByToken(String token) {
        int userid = jwtUtil.getUserIdByToken(token);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userid);
        Shop shop = shopMapper.selectOne(wrapper);
        return shop;
    }


    /**
     * 查询所有商店
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Shop> findallshop(Integer page, Integer size) {
        //页数
        if (page == null) {
            page = 1;
        }
        //条数
        if (size == null) {
            size = 5;
        }
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        Page<Shop> shopPage = shopMapper.selectPage(new Page<>(page, size),
                wrapper);
        return shopPage;
    }


    /**
     * 根据类型查询商家
     *
     * @param type
     * @return
     */
    public List<Shop> fingshopByType(String type) {
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("shoptype", type);
        List<Shop> shops = shopMapper.selectList(wrapper);
        for (Shop shop : shops) {
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shop_id", shop.getShopId());
            List<Goods> list = goodsMapper.selectList(queryWrapper);
            shop.setList(list);
        }
        return shops;
    }


    /**
     * 直接查询所有商店和商品
     *
     * @return
     */
    public List<Shop> findShop() {
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        List<Shop> shops = shopMapper.selectList(wrapper);
        for (Shop shop : shops) {
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shop_id", shop.getShopId());
            List<Goods> list = goodsMapper.selectList(queryWrapper);
            shop.setList(list);
        }
        return shops;
    }


    /**
     * 用户完成订单
     *
     * @param token
     * @param orderId
     * @return
     */
    public Boolean finishOrder(String token, Long orderId) {
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            QueryWrapper<Torder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", orderId);
            Torder torder = orderMapper.selectOne(wrapper);
            Notice notice = new Notice();
            notice.setSendid(userId);
            notice.setAcceptid(torder.getUserId());
            notice.setContent("订单已完成");
            notice.setCreatetime(System.currentTimeMillis());
            notice.setStatus(1);
            notice.setType(0);
            notice.setSendoraccept(0);
            noticeMapper.insert(notice);
            torder.setStatus(3);
            QueryWrapper<Torder> torderQueryWrapper = new QueryWrapper<>();
            torderQueryWrapper.eq("order_id", orderId);
            orderMapper.update(torder, torderQueryWrapper);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

    }


}
